package org.jader.bolao.gui.swing;

import java.awt.FlowLayout;
import java.awt.Frame;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jader.bolao.Championship;
import org.jader.bolao.IncorrectDataException;
import org.jader.bolao.Team;
import org.jader.bolao.Team.Modality;

public class NewTeamDialog extends JDialog {
	private static final long serialVersionUID = 1L;
	
	private Championship championship = null;
	private Team newTeam = null;
	
	private JPanel jContentPane = null;
	private JPanel labelPanel = null;
	private JPanel newTeamPanel = null;
	private JPanel buttonsPanel = null;
	private JLabel newTeamLabel = null;
	private JButton okButton = null;
	private JButton cancelButton = null;
	private JTextField newTeamTextField = null;
	private JComboBox newTeamModalityComboBox = null;
	
	/**
	 * @param owner
	 */
	public NewTeamDialog(Frame owner, Championship c) {
		super(owner);
		this.championship = c;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(334, 300);
		this.setTitle("Nova Equipe");
		this.setModalityType(ModalityType.APPLICATION_MODAL);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); //Avoid closing with [X]
		this.setResizable(false);
		this.setLocationRelativeTo(this.getParent());
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BoxLayout(getJContentPane(), BoxLayout.Y_AXIS));
			jContentPane.add(getLabelPanel(), null);
			jContentPane.add(getNewTeamPanel(), null);
			jContentPane.add(getButtonsPanel(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes labelPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getLabelPanel() {
		if (labelPanel == null) {
			newTeamLabel = new JLabel();
			newTeamLabel.setText("Criar nova equipe:");
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setAlignment(FlowLayout.LEFT);
			labelPanel = new JPanel();
			labelPanel.setLayout(flowLayout);
			labelPanel.add(newTeamLabel, null);
		}
		return labelPanel;
	}

	/**
	 * This method initializes gamesListPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getNewTeamPanel() {
		if (newTeamPanel == null) {
			FlowLayout flowLayout1 = new FlowLayout();
			flowLayout1.setAlignment(FlowLayout.LEFT);
			newTeamPanel = new JPanel();
			newTeamPanel.setLayout(flowLayout1);
			newTeamPanel.add(getNewTeamTextField(), null);
			newTeamPanel.add(getNewTeamModalityComboBox(), null);
		}
		return newTeamPanel;
	}

	private JComboBox getNewTeamModalityComboBox() {
		if (newTeamModalityComboBox == null) {
			newTeamModalityComboBox = new JComboBox(championship.getPossibleTeamModalities());
		}
		return newTeamModalityComboBox;
	}

	private JTextField getNewTeamTextField() {
		if (newTeamTextField == null) {
			newTeamTextField = new JTextField();
		}
		return newTeamTextField;
	}

	/**
	 * This method initializes buttonsPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getButtonsPanel() {
		if (buttonsPanel == null) {
			buttonsPanel = new JPanel();
			buttonsPanel.setLayout(new FlowLayout());
			buttonsPanel.add(getOkButton(), null);
			buttonsPanel.add(getCancelButton(), null);
		}
		return buttonsPanel;
	}

	/**
	 * This method initializes okButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("OK");
			okButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					// Try to create new team
					try {
						newTeam = championship.createTeam(getNewTeamTextField().getName(), (Modality) getNewTeamModalityComboBox().getSelectedItem());
					} catch (IncorrectDataException ex) {
						JOptionPane.showMessageDialog(null, ex.getMessage(), "Erro ao criar equipe", JOptionPane.ERROR_MESSAGE);
						return;
					}
					dispose();
				}
			});
		}
		return okButton;
	}

	/**
	 * This method initializes cancelButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancelar");
			cancelButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dispose();
				}
			});
		}
		return cancelButton;
	}
	
	public Team showDialog() {
		newTeam = null;
		this.setVisible(true);
		return newTeam;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
